package com.site.vo;

import com.site.common.BaseForm;
import com.site.common.validator.annotation.DateFormat;
import com.site.pojo.TestPojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "测试对象",description = "测试请求参数vo")
public class TestFormVo extends BaseForm<TestPojo> {

    // ** 注意 **：只要有一个字段使用了分组，其他所有的都必须加分组，不然会失效

    /**
     * 只在更新的时候验证
     */
    @ApiModelProperty(value = "id")
    @PositiveOrZero(groups = Update.class,message = "id必须是0或正整数")
    @NotNull(message = "id不能为空",groups = Update.class)
    private Integer id;

    @ApiModelProperty(value = "名称",name = "name")
    @Size(max = 32,message = "名称长度最大{max}")
    @NotBlank(message = "姓名不能为空",groups = {Create.class,Update.class})
    private String name;

    @NotNull(message = "状态不能为空",groups = {Create.class,Update.class})
    @Range(min = 0,max = 1,message = "状态值只能为{min}或{max}",groups = {Create.class,Update.class})
    private Integer status;

    @DateFormat(message = "日期不能为空",format = "yyyy-MM-dd HH:mm:ss",groups = {Create.class,Update.class})
    private Date time;

    /**
     * 获取实例
     * @return 返回实体类
     */
    @Override
    public TestPojo buildEntity() {
        TestPojo testPojo = new TestPojo();
        BeanUtils.copyProperties(this,testPojo);
        testPojo.setId(1000000);
        return testPojo;
    }
}
